/*
 *说明：资源信息表 增加/修改(网络设备)脚本
 *
 *@author liuxun 2013-05-06
*/

var opeForm;
var operationType;
var accNameOfNoAccMode = "noaccmode";	// 无账号模式下默认账户名称

$(function(){

	opeForm=$('#opeForm');
	operationType = $('#operationType').val();
	if (operationType == 0) {
		opeForm.url = 'C_RSC!doInsertNetworkDevices.do';
	} else if(operationType == 1){
		opeForm.url = 'C_RSC!doUpdateNetworkDevices.do';
		queryProtocolOperation();
	}else if(operationType == 6){
		opeForm.url = 'C_RSC!doInsertNetworkDevices.do';
		queryProtocolOperation();
	} 
	
	var rsc_type = $('#rsc_type').val();
	if(rsc_type == 'CISCO' || rsc_type == 'HUAWEI'){
		$('#en_pwd_tr').show();
	}else{
		$('#en_pwd_tr').hide();
	}

	$('#rsc_type').change(changeRscType);
	$('#is_modify_password').change(changePassword);
	$('#is_modify_en_pwd').change(changeENPassword);
	$('#showPass').change(passwordTypeChange);
	$('#showEditPass').change(editPasswordTypeChange);
	$('#no_acc_mode').change(changeNoAccMode);
	$('#okBtn').click(doOk);
	$('#cancelBtn').click(closeWin);
});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//选择资源组
function select_c_rsc_grp() {
	var node_id = $('#node_id').val();
	var action = 'C_RSC_GRP!showSelect.do?model.node_id=' + node_id;
	var row = showModalWin(action,'A2');
	
	if (row) {
		$('#rsc_grp_id').val(row.rsc_grp_id);
		$('#rsc_grp_name').val(row.rsc_grp_name);
	}
}

function changeNoAccMode() {
	var isCheck = $('#no_acc_mode').attr('checked');
	if (isCheck) {
		$('#manage_acc').attr('readonly', true);
		$('#manage_acc').val(accNameOfNoAccMode);
	} else {
		$('#manage_acc').attr('readonly', false);
		$('#manage_acc').val('');
	}
}

//选中“修改密码”复习框，取消密码输入框禁用状态
function changePassword() {
	var isCheck = $('#is_modify_password').attr('checked');
	var showEditPass = $("#showEditPass").attr("checked");
	if (isCheck) {
		//$('#password').attr('disabled', false);
		if(showEditPass){
			document.getElementById('editPass').innerHTML='<input type="text" id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="" maxlength="50">';
		}else{
			document.getElementById('editPass').innerHTML='<input type="password" id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="" maxlength="50">';
		}
	} else if(!isCheck){
		$('#password').val('');
		$("#showEditPass").attr("checked", false);
		$('#password').attr('disabled', true);
	}
}

//修改时 逻辑判断是否明文显示密码框
function editPasswordTypeChange(){
	var temp = document.getElementById('password').value;
	var isCheck = $('#is_modify_password').attr('checked');
	var showEditPass = $("#showEditPass").attr("checked");
	
	if(showEditPass && isCheck){
		document.getElementById('editPass').innerHTML='<input type="text" id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="${model.password}" maxlength="50">';
	}else if(!showEditPass && isCheck){
		document.getElementById('editPass').innerHTML='<input type=password id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="${model.password}" maxlength="50">';
	} 
	document.getElementById('password').value=temp;
}

//新增时用到
function passwordTypeChange(){
	var temp = document.getElementById('password').value;
	if($("#showPass").attr("checked")){
		document.getElementById('pass').innerHTML='<input type="text" id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="${model.password}" maxlength="50"> &nbsp;';
	}else{
		document.getElementById('pass').innerHTML='<input type=password id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="${model.password}" maxlength="50"> &nbsp;';
	} 
	document.getElementById('password').value=temp;
}


//EN密码，选中“修改密码”复习框，取消密码输入框禁用状态
function changeENPassword() {
	var isCheck = $('#is_modify_en_pwd').attr('checked');
	if (isCheck) {
		$('#en_passwd').val('');
		$('#en_passwd').attr({style:"display:block"});
		$('#en_passwd').attr('disabled', false);
		$('#en_passwd').attr('required', true);
	} else {
		$('#en_passwd').val('');
		$('#en_passwd').attr({style:"display:none"});
		$('#en_passwd').attr('disabled', true);
		$('#en_passwd').attr('required', false);
	}
}

//显示特权账号可编辑状态
function privilegeChange(){
	if($("#showPrivilege").attr("checked")){
		//可编辑文本框
		$('#super_acc').val('');
		$('#super_acc').attr('disabled', true);
		$('#super_acc').attr({style:"display:none"});
		$('#privilege2').attr('disabled', false);
		$('#privilege2').attr({style:"display:block"});
		$('#privilege2').attr('required', true);
	}else{
		//选择列表
		$('#super_acc').attr('disabled', false);
		$('#super_acc').attr({style:"display:block; width:150px"});
		$('#privilege2').val('');
		$('#privilege2').attr('disabled', true);
		$('#privilege2').attr({style:"display:none"});
		$('#privilege2').attr('required', false);
	} 
}

function changeRscType() {
	showOrHideEnPwd();
	queryProtocolOperation();
}

function itemOnChange(){
	$('#rsc_grp_id').val('');
	$('#rsc_grp_name').val('');
}

function showOrHideEnPwd(){
	var rsc_type = $('#rsc_type').val();
	if(rsc_type == 'CISCO'){
		$('#en_pwd_tr').show();
		$('#en_passwd').attr('disabled', false);
		$('#en_passwd').attr('required', true);
	}else{
		$('#en_passwd').attr('disabled', true);
		$('#en_passwd').attr('required', false);
		$('#en_pwd_tr').hide();
	}
}

//查询操作
function queryProtocolOperation(){
	var rsc_type = $('#rsc_type').val();
	if (isEmpty(rsc_type)) {
		showInfoWin('资源类型不允许为空！');
		return;
	} else {
		var rsc_id = '';
		if (isNotEmpty($('#rsc_id').val())) {
			rsc_id = $('#rsc_id').val();
		}
	}
	
	var params = {'model.rsc_id': rsc_id, 'model.rsc_type': rsc_type};
	$.ajax({
		type : "get",
		dataType : "json",
		url : "C_RSC!querySelectedAndNotSelectedProtocolList.do",
		data : params,
		success:function(data){ 	
			clearRowsSMS();
			var rows = data.rows;
			$.each(rows, function(i, n) {
				//var isSelect = (n.IS_SELECTED == '1' ? 'checked': '');
				var str_ssh = 'ssh';
				var str_telnet = 'telnet';
				var defaultSelect;
				if(operationType == '0'){
					if(str_ssh.toUpperCase() == n.PROTOCOL_NAME.toUpperCase() || str_telnet.toUpperCase() == n.PROTOCOL_NAME.toUpperCase()){
						defaultSelect = 'checked';
					}					
				}else{
					defaultSelect = (n.IS_SELECTED == '1' ? 'checked': '');
				}
				
				// onClick="return false" readonly="readonly"
				var readonly = false;
				if(operationType == '1' && defaultSelect == 'checked'){
					var str = '<tr>' +
					'<td><input onClick="return false" type="checkbox" ' +  defaultSelect + '  /></td>' +	
					'<td align="center">' + n.PROTOCOL_NAME + '<input type="hidden" id="protocol" value="' + n.PROTOCOL + '" /></td>' +
					'<td align="center" ><input type="text" id="port" validType="digit" class="easyui-validatebox  editMainText" value="' + n.PORT + '" with="30%" /></td>' +
				'</tr>';
				}else{
					var str = '<tr>' +
					'<td><input type="checkbox" ' +  defaultSelect + '  /></td>' +	
					'<td align="center">' + n.PROTOCOL_NAME + '<input type="hidden" id="protocol" value="' + n.PROTOCOL + '" /></td>' +
					'<td align="center" ><input type="text" id="port" validType="digit" class="easyui-validatebox  editMainText" value="' + n.PORT + '" with="30%" /></td>' +
				'</tr>';
				}
				

				$("#resultTable").find('tbody').append(str);
			});
		}   
	});
                
}

//清除Table
function clearRowsSMS(){
	var tableLen = document.getElementById('resultTable').rows.length;
	if(tableLen > 1){
		for(var i=0; i<tableLen-1; i++){
			document.getElementById('resultTable').deleteRow(tableLen-i-1);
		}
	}
}

//保存按钮操作
function doOk() {
	var validateResult = opeForm.form('validate');

	if (!validateResult){
		showInfoWin('请正确填写信息!');
		return;
	}
	
	var isCheck = $('#is_modify_password').attr('checked');
	if (isCheck) {
		var password = $('#password').val();
		if (isEmpty(password)) {
			showInfoWin('你选择修改口令，请输入新口令!');
			return;
		}
	}
	
	var enPwdIsCheck = $('#is_modify_en_pwd').attr('checked');
	if (enPwdIsCheck) {
		var en_passwd = $('#en_passwd').val();
		if (isEmpty(en_passwd)) {
			showInfoWin('你选择修改EN口令，请输入新EN口令!');
			return;
		}
	}
	
	var protocalArr = [];
	var portArr = [];
	
	var defauleSelected = false;

	$('#resultTable tbody tr').each(function(i,n){ 
		var isCheck = $(this).find('td').eq(0).find('input[type="checkbox"]').attr('checked');
		if (isCheck) {
			var protocal = $(this).find('td').eq(1).find('input[id="protocol"]').val();
			
			if('0' == protocal.toUpperCase() || '3' == protocal.toUpperCase()){
				defauleSelected = true;
			}
			var port = $(this).find('td').eq(2).find('input[id="port"]').val();
			protocalArr.push(protocal);
			portArr.push(port);
		}
	}); 
	
	if(!defauleSelected){
		showInfoWin('资源协议类型有误！');
		return;
	}

	var protocols = $ .param({'model.pro_id' : protocalArr}, true);
	var ports = $ .param({'model.port' : portArr}, true);
	showSystemProccessing();
	var options = { 
		url: opeForm.url + '?' +  protocols + '&' + ports,
		type: 'post',
		success: function(data){
			removeSystemProccessing();
			getOpenerWin().afterOK();

		    if (operationType == '0') {
				showInfoWin('操作成功!', refreshWin);
			} else {
				showInfoWin('操作成功!', function() {window.close();});
			}
		}
	};

	opeForm.ajaxSubmit(options);
}